package com.example.com.skills_utf8;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.com.skills_utf8._42RotateAnimation.InterpolatedTimeListener;

public class _42MainActivity extends Activity{

	
	_42MainActivity activity = this;
	
	Timer timer = new Timer();
	
	ArrayList<String> listdata = new ArrayList<String>();

	private Adapter adapter;

	private GridView gridview;
	
	

    private Interpolator accelerator = new AccelerateInterpolator();
    private Interpolator decelerator = new DecelerateInterpolator();

	 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_42);
		
		final LinearLayout layout_content = (LinearLayout)this.findViewById(R.id.layout_content);
		final LinearLayout layout_content1 = (LinearLayout)findViewById(R.id.layout_content1);
		final ImageView iv = (ImageView)findViewById(R.id.iv);
		final ImageView iv1 = (ImageView)findViewById(R.id.iv1);
		
		gridview = (GridView)findViewById(R.id.grid_view);
		
		
		
		timer.schedule( new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				
				
				activity.runOnUiThread( new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						
						/*
						_42RotateAnimation rotateAnim = null;
						float cX = iv.getWidth() / 2.0f;  // 0
						float cY = iv.getHeight() / 2.0f;
						System.out.println("cX:"+cX);
						System.out.println("cY:"+cY);
						
						rotateAnim = new _42RotateAnimation(cX, cY, _42RotateAnimation.ROTATE_DECREASE);
						if (rotateAnim != null) {
							rotateAnim.setInterpolatedTimeListener( new InterpolatedTimeListener() {
								
								@Override
								public void interpolatedTime(float interpolatedTime) {
									// TODO Auto-generated method stub
//									// 监听到翻转进度过半时，更新txtNumber显示内容。
										Log.d("RotateAnimation", "interpolatedTime:" + interpolatedTime );
//									}
								}
							});
							rotateAnim.setFillAfter(true);
							iv.startAnimation(rotateAnim);
						}
						*/
						
						flipit(iv, iv);
						flipit(iv1, iv1);
						

					 /*   
						 ObjectAnimator visToInvis = ObjectAnimator.ofFloat(iv, "rotationY", 0f, 90f);
					        visToInvis.setDuration(1500);
					        visToInvis.setInterpolator(accelerator);
					        visToInvis.addListener(new AnimatorListenerAdapter() {  
					            @Override
					            public void onAnimationEnd(Animator anim) {
					            }
					        });
					        visToInvis.start();
						*/
				
						
						// 旋转view
				//		adapter.notifyDataSetChanged();
						
					}
				});
				
				
						
			}
		}, 1000, 10000);
		
		
		
		
		
	
		for(int i =0; i<4; i++){
			listdata.add(""+i);
		}
		
		adapter = new Adapter();
		gridview.setAdapter(adapter);
		
		
		
	}
	
    private void flipit(final View visibleList, final View invisibleList) {
    	/*
        final ListView visibleList;
        final ListView invisibleList;
        if (mEnglishList.getVisibility() == View.GONE) {
            visibleList = mFrenchList;
            invisibleList = mEnglishList;
        } else {
            invisibleList = mFrenchList;
            visibleList = mEnglishList;
        }
        */
    	
        ObjectAnimator visToInvis = ObjectAnimator.ofFloat(visibleList, "rotationY", 0f, 90f);
        visToInvis.setDuration(1500*3);
        visToInvis.setInterpolator(accelerator);
        final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(invisibleList, "rotationY",
                -90f, 0f);
        invisToVis.setDuration(1500*3);
        invisToVis.setInterpolator(decelerator);
        visToInvis.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator anim) {
                visibleList.setVisibility(View.GONE);
                invisToVis.start();
                invisibleList.setVisibility(View.VISIBLE);
            }
        });
        visToInvis.start();
    }
    
	
	
	class Adapter extends BaseAdapter{

		Holder holder = new Holder();
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listdata.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listdata.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			
			convertView = activity.getLayoutInflater().inflate(
					R.layout.activity_42_gridview_item, null);
			
			ImageView iv = (ImageView)convertView.findViewById(R.id.iv);
			
			/*
			_42RotateAnimation rotateAnim = null;
			float cX = iv.getWidth() / 2.0f;  // 0
			float cY = iv.getHeight() / 2.0f;
				rotateAnim = new _42RotateAnimation(cX, cY, _42RotateAnimation.ROTATE_DECREASE);
			if (rotateAnim != null) {
				rotateAnim.setInterpolatedTimeListener( new InterpolatedTimeListener() {
					
					@Override
					public void interpolatedTime(float interpolatedTime) {
						// TODO Auto-generated method stub
//						// 监听到翻转进度过半时，更新txtNumber显示内容。
							Log.d("RotateAnimation", "interpolatedTime:" + interpolatedTime );
//						}
					}
				});
				rotateAnim.setFillAfter(true);
				iv.startAnimation(rotateAnim);
			}
			*/
			
			
			
			
			return convertView;
		}
		
		
		class Holder{
			
		};
		
	};
	
	
	
	
	
}
